function formatPrice(val){
	if (val < 1){
		return '<span style="color:red;">停用</span>';
	} else {
		return '<span style="color:blue;">启用</span>';
	}
}
function ajax(url,type, data,contentType,success,err){
	$.ajax({ url: url,
	     type:type,
	     contentType : contentType, 
	     data:data,
	     success: function(err){
	    	 if(success)success(err);
         },
         error : function(error){
        	 if(err)err(error);
         }
     });
}
function doQueryForm(){
	$('#queryForm').form('submit',{
		url:qxgl.paserUrl('user/queryUserform'),
		success:function(list){
			var data=JSON.parse(list);
			$("#usrManager").datagrid("loadData",data);
		}
	});
}
$(function(){
    $('#createdate').datebox({
    	formatter:function(date){
    		var y = date.getFullYear();
    		var m = date.getMonth()+1;
    		var d = date.getDate();
    		return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
    	},
    	parser:function(s){
    		if (!s) return new Date();
    		var ss = (s.split('-'));
    		var y = parseInt(ss[0],10);
    		var m = parseInt(ss[1],10);
    		var d = parseInt(ss[2],10);
    		if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
    			return new Date(y,m-1,d);
    		} else {
    			return new Date();
    		}
    	}
    });
	$('#doQuery').click(function(){
		doQueryForm();
	});
	
	$('#doCreate').click(function(){
		showMessageDialog(qxgl.paserUrl('user/create'),'新建',500,300,true);
	});
	$('#doCompile').click(function(){
		var user=$('#usrManager').datagrid('getSelected');
		if(!user){
			$.messager.alert('','请选择一条数据!','info');
		}else{
			var url=qxgl.paserUrl('user/compile');
			url+='?username='+user.username;
			showMessageDialog(url,'编辑',500,300,true);
		}
	});
	$('#doDelete').click(function(){
		var user=$('#usrManager').datagrid('getSelected');
		if(!user){
			$.messager.alert('','请选择一条数据!','info');
		}else{
			var url=qxgl.paserUrl('user/delete');
			//url+='?username='+user.username;
			ajax(url,'get',"username="+user.username,'json',function(err){
				if(err){
					$.messager.alert('','删除失败!','error');
				}else{
					$.messager.alert('','删除成功!','info');
					doQueryForm();
				}
			},function(err){
				if(err){
					$.messager.alert('','删除失败!','error');
				}else{
					$.messager.alert('','删除成功!','info');
					doQueryForm();
				}
			});
		}
	});
	$('#doClear').click(function(){
		$('#queryForm').form('clear');
	});
	//url：窗口调用地址，title：窗口标题，width：宽度，height：高度，shadow：是否显示背景阴影罩层  
	function showMessageDialog(url, title, width, height, shadow) {  
	    var content = '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="no"></iframe>';  
	    var boarddiv = '<div id="msgwindow" title="' + title + '"></div>'//style="overflow:hidden;"可以去掉滚动条  
	    $(document.body).append(boarddiv);  
	    var win = $('#msgwindow').dialog({  
	        content: content,  
	        width: width,  
	        height: height,  
	        modal: shadow,  
	        title: title,  
	        onClose: function () {  
	            $(this).dialog('destroy');//后面可以关闭后的事件  
	            doQueryForm();
	        }  
	    });  
	    win.dialog('open');  
	}
});